package com.cking.wherehaveibeen;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PointDataSource {
	  // Database fields
	  public static final String TABLE_COMMENTS = "points";
	  public static final String COLUMN_ID = "_id";
	  public static final String COLUMN_POINTS = "point";
	  public static final String COLUMN_DATE = "date";

	  private static final String DATABASE_NAME = "mydb";
	  private static final int DATABASE_VERSION = 1;

	  // Database creation sql statement
	  private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS "
	      + TABLE_COMMENTS + " (" + COLUMN_ID
	      + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ COLUMN_DATE
	      + " TEXT NOT NULL, " + COLUMN_POINTS
	      + " TEXT NOT NULL);";
	  private SQLiteDatabase database;
	  private MySQLHelper dbHelper;
	  private final Context ctx;
	  private String[] allColumns = { COLUMN_ID, COLUMN_DATE, COLUMN_POINTS };

	  public PointDataSource(Context context) {
		  ctx = context;
		  dbHelper = new MySQLHelper(context);
	  }
	  
	  private static class MySQLHelper extends SQLiteOpenHelper {
		  
		  public MySQLHelper(Context context) {
			    super(context, DATABASE_NAME, null, DATABASE_VERSION);
		  }
		  
		  @Override
		  public void onCreate(SQLiteDatabase database) {
			try {
	      		database.execSQL(DATABASE_CREATE);	
	      	} catch (SQLException e) {
	      		e.printStackTrace();
	      	}
		  }

		  @Override
		  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		    Log.w(MySQLHelper.class.getName(),
		        "Upgrading database from version " + oldVersion + " to "
		            + newVersion + ", which will destroy all old data");
		    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
		    onCreate(db);
		  }

		} 

	  public PointDataSource open() throws SQLException {
	    dbHelper = new MySQLHelper(ctx);
	    database = dbHelper.getWritableDatabase();
	    return this;
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Point createPoint(String point) {
		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_MONTH);
		int month = c.get(Calendar.MONTH);
		int year = c.get(Calendar.YEAR);
	    ContentValues values = new ContentValues();
	    values.put(COLUMN_POINTS, point);
	    values.put(COLUMN_DATE, day + "-" + month + "-" + year);
	    long insertId = database.insert(TABLE_COMMENTS, null, values);
	    Cursor cursor = database.query(TABLE_COMMENTS,
	        allColumns, COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Point newPoint = cursorToPoint(cursor);
	    cursor.close();
	    return newPoint;
	  }

	  public void deletePoint(Point point) {
	    long id = point.getId();
	    System.out.println("Point deleted with id: " + id);
	    database.delete(TABLE_COMMENTS, COLUMN_ID
	        + " = " + id, null);
	  }

	  public List<Point> getAllPoints() {
	    List<Point> points = new ArrayList<Point>();

	    Cursor cursor = database.query(TABLE_COMMENTS,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Point point = cursorToPoint(cursor);
	      points.add(point);
	      cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return points;
	  }

	  private Point cursorToPoint(Cursor cursor) {
	    Point point = new Point();
	    point.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
	    point.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
	    point.setPoint(cursor.getString(cursor.getColumnIndex(COLUMN_POINTS)));
	    return point;
	  }
	} 
